// ==UserScript==
// @name           	Msg Frame GNN
// @namespace      	falcons.marknas.com
// @author			Carolus Rex, 2011
// @description    	Adds the latest GNN entry to the message frame
// @include        	http://*.pardus.at/msgframe.php*
// @version		   	0.7.0

var universe = document.getElementById('universe').getAttribute('title');
universe = universe.substring(0, universe.indexOf(":")).toLowerCase();

var gnnText = ""
var latestTweetId = "";
if(localStorage.getItem('tweetSinceId') != null){
	latestTweetId = localStorage.getItem('tweetSinceId');
}
var url = "http://search.twitter.com/search.json?&q=from:pardus" + universe + "&rpp=1&since_id=" + latestTweetId;

GM_xmlhttpRequest({
  method: "GET",
  url: url,
  onload: function(response) {
	processTweet(response.responseText);
  }
});

function processTweet(jsonData){
	var shtml = "";
	var tweetID = ""
	var results = JSON.parse(jsonData).results;
	if (results.length > 0){
		shtml = "GNN, " + results[0].created_at.replace("+0000", "GMT") + ": " + results[0].text;
		tweetID = results[0].id_str;
		if (shtml != ""){
			localStorage.setItem('gnnMessage', shtml);
		}
		if (tweetID != null){
			localStorage.setItem('tweetSinceId', tweetID);
		}
	}

}

if(localStorage.getItem('gnnMessage') != null){
	var div = document.createElement("div");
	div.id = "tweetDiv";
	div.innerHTML = localStorage.getItem('gnnMessage');
	document.getElementsByTagName('td')[1].appendChild(div);
}

// ==/UserScript==